package com.wgts.wgts.mvp.view.activity.moremore;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.mvp.view.activity.awardmore.MyAwardMoreActivity;
import com.wgts.wgts.tools.MoHuGlide;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

public class OpenMoreMoreDialog extends DialogFragment {
    @BindView(R.id.img_bg)
    ImageView imgBg;
    @BindView(R.id.img_head)
    CircleImageView imgHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_open_info)
    TextView tvOpenInfo;
    Unbinder unbinder;
    @BindView(R.id.img_ensure)
    ImageView imgEnsure;

    private static final String IMGEURL = "IMGEURL";

    private static final String USERNAME = "USERNAME";

    private static final String PRODUCTINFO = "PRODUCTINFO";

    private String imgUrl;

    private String productInfo;

    private String userName;


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog alertDialog = new Dialog(getContext());
        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        View customView = onCreateDialogView();
        alertDialog.setContentView(customView);
        alertDialog.setCanceledOnTouchOutside(true);

        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        alertDialog.getWindow().setWindowAnimations(R.style.DialogOutAndInStyle);
        WindowManager.LayoutParams windowParams = alertDialog.getWindow().getAttributes();
        windowParams.width = (int) (getResources().getDisplayMetrics().widthPixels -
                getResources().getDisplayMetrics().density * 20);
        alertDialog.getWindow().setAttributes(windowParams);

        return alertDialog;
    }

    public View onCreateDialogView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.open_more_more_dialog, null);
        unbinder = ButterKnife.bind(this, view);
        initBundle();
        initView();
        return view;
    }

    private void initBundle(){
        imgUrl = getArguments().getString(IMGEURL);
        userName = getArguments().getString(USERNAME);
    }

    private void initView() {

        MoHuGlide.getInstance().glideImg(getContext(), imgUrl, imgHead);
        tvName.setText("中奖人:" + userName);


        ObjectAnimator ra = ObjectAnimator.ofFloat(imgBg, "rotation", 0f, 359f, 0f);
        ra.setDuration(20000);
        ra.setInterpolator(new LinearInterpolator());
        ra.setRepeatCount(-1);//设置重复次数
        ra.start();
    }

    public static OpenMoreMoreDialog getInstacne(String imgUrl, String userName) {
        OpenMoreMoreDialog moreMoreDialog = new OpenMoreMoreDialog();
        Bundle bundle = new Bundle();
        bundle.putString(IMGEURL, imgUrl);
        bundle.putString(USERNAME, userName);
        moreMoreDialog.setArguments(bundle);
        return moreMoreDialog;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }



    @OnClick(R.id.img_ensure)
    public void onViewClicked() {
        Intent intent = new Intent(getContext(), MyAwardMoreActivity.class);
        getContext().startActivity(intent);
        getActivity().finish();
    }
}
